package com.database.controller;

import com.database.character.Praise;
import com.database.character.Result;
import com.database.service.PraiseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/praises")
@Api(value = "点赞controller",tags = {"点赞相关接口"})
public class PraiseController {
	@Autowired
	private PraiseService praiseService;
	/*点赞*/
	@PostMapping
	@ApiOperation(value = "点赞",notes = "点赞帖子",httpMethod = "POST")
	@ApiResponses(
			@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	)
	@ApiImplicitParam(name = "token",value = "token",required = true,dataTypeClass = String.class,paramType = "header")
	public Result addPraise(@RequestBody Praise praise){
		Integer userId = praise.getUserId();
		Integer postId = praise.getPostId();
		int i = praiseService.addPraise(praise);
		if(i==0){
			return Result.error("点赞失败");
		}
		else{
			Praise praiseByUidAndPid = praiseService.getPraiseByUidAndPid(userId, postId);
			return Result.success("点赞成功",praiseByUidAndPid);
		}
	}
	/*取消点赞*/
	@DeleteMapping("/{userId}/{postId}")
	@ApiOperation(value = "取消点赞",notes = "取消点赞帖子",httpMethod = "DELETE")
	@ApiResponses(
			@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result deletePraise(@PathVariable Integer userId,@PathVariable Integer postId){
		int i = praiseService.deleteByUidAndPid(userId,postId);
		if(i==0){
			return Result.error("取消失败");
		}
		else{
			return Result.success("取消成功",null);
		}
	}
	/*查看赞过的帖子*/
	@GetMapping("/{userId}")
	@ApiOperation(value = "查看赞过的帖子",notes = "查看赞过的帖子",httpMethod = "GET")
	@ApiResponses(
			@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result getPraise(@PathVariable Integer userId){
		List<Praise> praiseByUid = praiseService.getPraiseByUid(userId);
		return Result.success("查询成功",praiseByUid);
	}

	/*查看该帖子是否被赞过*/
	@GetMapping("/{userId}/{postId}")
	@ApiOperation(value = "查看该帖子是否被赞过",notes = "查看该帖子是否被赞过",httpMethod = "GET")
	@ApiResponses(
			@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "postId",value = "帖子id",required = true,paramType = "path",dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "token",value = "token",required = true,paramType = "header",dataTypeClass = String.class)
	})
	public Result judgePraise(@PathVariable Integer userId,@PathVariable Integer postId){
		Praise praiseByUid = praiseService.getPraiseByUidAndPid(userId,postId);
		return Result.success("查询成功",praiseByUid);
	}
}
